'''
牛客网刷题
1.BM1 反转链表
2.BM2 链表内滞定区间反转
'''

class NodeList:
    def __init__(self, val, next=None):
        self.val = val
        self.next = next



class Solution:
    def reverse(self, node: NodeList)->NodeList:
        pre = None
        head = node
        while head:
            next = head.next
            head.next = pre
            pre = head
            head = next
        return pre

    def reverseBetween(self,head:NodeList,m:int,n:int)->NodeList:
        res = NodeList(-1)
        res.next = head
        pre = res
        cur = head
        for i in range(1,m):
            pre = cur
            cur = cur.next

        for i in range(m,n):
            temp = cur.next
            cur.next = temp.next
            temp.next = pre.next
            pre.next = temp

        return res.next

def printList(node: NodeList):
    while node:
        print(node.val, end="->" if node.next else "\n")
        node = node.next


nodelist = NodeList(1, NodeList(2, NodeList(3, NodeList(4, NodeList(5)))))




print("原始链表")
printList(nodelist)
#
# print("反转后的链表")
# solution = Solution().reverse(nodelist)
# printList(solution)


# solutionBetween = Solution().reverseBetween(nodelist,2,4)
# printList(solutionBetween)